﻿using FlaUI.Core.AutomationElements;
using FlaUI.Core.Conditions;
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;
using FlaUI.UIA3;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using FlaUI.Core.Capturing;

namespace WindowsWechat
{
    public partial class Form1 : Form
    {
        int sleepTime = 500;//等待时间
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.ALT, VirtualKeyShort.KEY_Q });//全选 

            lab_msg.ForeColor = Color.Green;
            Process[] processes = Process.GetProcessesByName("WeChat");
            if (processes.Count() != 1)
            {
                Console.WriteLine("微信未启动或启动多个微信");
            }
            else
            {
                //1.附加到微信进程
                using (var app = FlaUI.Core.Application.Attach(processes.First().Id))
                {
                    using (var automation = new UIA3Automation())
                    {
                        //2.获取主界面
                        var mainWindow = app.GetMainWindow(automation);

                        //搜搜框--全选--删除--输入内容--回车--切换输入框--输入文字--发送
                        //var img = mainWindow.FindAllByXPath("/Pane[2]/Pane/Pane[2]/Pane[1]/Pane/Image").FirstOrDefault();
                        ////img.DrawHighlight(System.Drawing.Color.Red);

                        //var captureImage = FlaUI.Core.Capturing.Capture.Element(img);
                        //captureImage.ToFile(@"d:/1.jpg");


                        //搜搜框--全选--删除--输入内容--回车--切换输入框--输入文字--发送
                        var Obj = mainWindow.FindAllByXPath("/Pane[3]/Pane[2]/Pane[2]/Pane[1]/Pane[1]/Pane/Edit").FirstOrDefault();
                        //Obj.DrawHighlight(System.Drawing.Color.Red);
                        Obj.Click();

                        //全选--删除
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A });//全选  
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.DELETE });//删除 

                        ////输入内容  
                        System.Windows.Forms.Clipboard.SetText(textBox1.Text.Trim());//内容复制到粘贴板
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });//ctrl+v  
                        //System.Threading.Thread.Sleep(500);

                        //var searchList = mainWindow.FindAllByXPath("/Pane[3]/Pane[2]/Pane[2]/Pane[2]/Pane/List");
                        //foreach (var item in searchList)
                        //{
                        //    var temp = item.FindAllDescendants().FirstOrDefault(cf => cf.Name == textBox1.Text.Trim() && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
                        //    if (temp != null)
                        //    {
                        //        temp.AsListBoxItem().DrawHighlight(System.Drawing.Color.Red);
                        //        temp.AsListBoxItem().Click();
                        //        System.Threading.Thread.Sleep(sleepTime);
                        //    }
                        //}

                        //回车
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.ENTER });//回车   
                        System.Threading.Thread.Sleep(500);
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.ENTER });//回车

                        ////5.切换到对话框
                        //lab_msg.Text = "3.切换到对话框";
                        //var searchList = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索结果"));
                        //if (searchList != null)
                        //{
                        //    var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
                        //    searchItem?.DrawHighlight(System.Drawing.Color.Red);
                        //    searchItem?.AsListBoxItem().Click();
                        //    System.Threading.Thread.Sleep(sleepTime);
                        //}
                        //else
                        //{
                        //    lab_msg.Text = "没有搜索到内容";
                        //    lab_msg.ForeColor = Color.Red;
                        //}

                        //定位输入框
                        var Edit = mainWindow.FindAllByXPath("/Pane[3]/Pane[2]/Pane[3]/Pane/Pane/Pane/Pane/Pane[2]/Pane[2]/Pane[2]/Pane/Pane[1]/Edit").FirstOrDefault();
                        Edit.Click();

                        //输入内容
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_A });//全选  
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.DELETE });//删除 

                        string sendMsg = richTextBox1.Text.Trim() + "\n时间:" + DateTime.Now.ToString();
                        System.Windows.Forms.Clipboard.SetText(sendMsg);
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
                        //Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.ENTER });//ctrl+enbter 发送
                    }
                }
            }
        }
    }
}
